package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tgky.ybzb.common.PageParam;
//import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.Jcyb;
import com.tgky.ybzb.entity.Jcybj;
import com.tgky.ybzb.entity.Rwal;
import com.tgky.ybzb.entity.Rwalj;
import com.tgky.ybzb.mapper.RwalMapper;
import com.tgky.ybzb.mapper.RwaljMapper;
import com.tgky.ybzb.service.IRwalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgky.ybzb.service.IRwaljService;
import com.tgky.ybzb.utils.MPUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务案例表 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class RwalServiceImpl extends ServiceImpl<RwalMapper, Rwal> implements IRwalService {
    @Resource
    RwalMapper rwalMapper;

    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param rwal 实体信息
     * @return
     */
    @Override
    public QueryWrapper<Rwal> getQueryWrapper(PageParam pageParam, Rwal rwal){
        QueryWrapper<Rwal> queryWrapper = MPUtil.getQueryWrapper(pageParam);
        queryWrapper.orderByDesc("create_time");
        if(rwal == null){
            return queryWrapper;
        }

        //主键
        if(rwal.getId() != null){
            queryWrapper.lambda()
                    .eq(Rwal::getId, rwal.getId());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
//                .eq(rwal.getYrwid() != null, Rwal::getYrwid, rwal.getYrwid())
                .eq(rwal.getWxbs() != null, Rwal::getWxbs, rwal.getWxbs())
                .eq(rwal.getCgqbs() != null, Rwal::getCgqbs, rwal.getCgqbs())
                .eq(rwal.getYxj() != null, Rwal::getYxj, rwal.getYxj())
                .eq(rwal.getGckssj() != null, Rwal::getGckssj, rwal.getGckssj())
                .eq(rwal.getGcjssj() != null, Rwal::getGcjssj, rwal.getGcjssj())
                .eq(rwal.getZxdfwsk() != null, Rwal::getZxdfwsk, rwal.getZxdfwsk())
                .eq(rwal.getJd() != null, Rwal::getJd, rwal.getJd())
                .eq(rwal.getWd() != null, Rwal::getWd, rwal.getWd())
                .eq(rwal.getCbj() != null, Rwal::getCbj, rwal.getCbj())
                .eq(rwal.getTygdj() != null, Rwal::getTygdj, rwal.getTygdj())
                .eq(rwal.getSsjcybjID() != null, Rwal::getSsjcybjID, rwal.getSsjcybjID());
        return queryWrapper;
    }

    /**
     * 保存任务案例并且统计任务总数
     * @param rwal
     * @return
     */
    @Override
    public boolean saveRwalAndCount(Rwal rwal) {
        int status=rwalMapper.insert(rwal);
        updateRwaljCount(rwal);
        return status==1 ? true:false;
    }

    /**
     * 删除任务案例并更新案例总数
     * @param ids
     * @return
     */
    @Override
    public boolean removeRwalAndCount(List<Long> ids) {
        Rwal rwal=rwalMapper.selectById(ids.get(0));
        int status=rwalMapper.deleteBatchIds(ids);
        updateRwaljCount(rwal);
        return status==1 ? true:false;
    }

    /**
     * 更新任务案例集数量
     * @param rwal
     */
    public synchronized void updateRwaljCount(Rwal rwal){
        // 统计决策样本数量
        QueryWrapper<Rwal> jcybQueryWrapper=MPUtil.getQueryWrapper();
        jcybQueryWrapper.eq("ssjcybjID",rwal.getSsjcybjID());
        Long count=rwalMapper.selectCount(jcybQueryWrapper);
        //更新决策样本集数量
        rwalMapper.updateRwaljSl(rwal.getSsjcybjID(),count);
    }
}
